package com.gpframework.module.common.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.common.constant.CacheConstants;
import com.gpframework.common.constant.Constants;
import com.gpframework.common.core.redis.RedisCache;
import com.gpframework.common.enums.CommonStatus;
import com.gpframework.common.enums.LabelType;
import com.gpframework.common.exception.BaseException;
import com.gpframework.common.utils.MessageUtils;
import com.gpframework.module.brand.domain.BrandStoreLabelRela;
import com.gpframework.module.brand.service.IBrandStoreLabelRelaService;
import com.gpframework.module.common.domain.CommonLabel;
import com.gpframework.module.common.domain.req.CommonLabelReq;
import com.gpframework.module.common.domain.req.CommonLabelUpdate;
import com.gpframework.module.common.domain.vo.CommonLabelVo;
import com.gpframework.module.common.mapper.CommonLabelMapper;
import com.gpframework.module.common.service.ICommonLabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName CommonLabel
 * @Author gpframework
 * @Date 2022-04-07
 * @Version 1.0.0
 * @Description 标签Service业务层处理
 */
@Service
public class CommonLabelServiceImpl extends ServiceImpl<CommonLabelMapper, CommonLabel> implements ICommonLabelService {

    @Autowired
    private IBrandStoreLabelRelaService brandStoreLabelRelaService;

    @Autowired
    private RedisCache redisCache;

    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2022-04-07
     * @Param [lqw]
     * @Return List<CommonLabelVo>
     */
    @Override
    public List<CommonLabelVo> listVo(QueryWrapper<CommonLabel> lqw) {
        return this.baseMapper.listVo(lqw);
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2022-04-07
     * @Param [id]
     * @Return CommonLabelVo
     */
    @Override
    public CommonLabelVo getVoById(Long id) {
        return this.baseMapper.getVo(id);
    }

    /**
     * 添加标签
     *
     * @param commonLabel
     * @return
     */
    @Override
    @Transactional
    public boolean saveLabel(CommonLabel commonLabel) {
        if (!commonLabel.getBusinessType().equals(LabelType.STORE_LABEL.getKey())) {
            this.baseMapper.insert(commonLabel);
        }
        // 添加门店关联关系
        if (commonLabel.getBusinessType().equals(LabelType.STORE_LABEL.getKey())) {
            CommonLabelReq labelReq = Convert.convert(new TypeReference<CommonLabelReq>() {
            }, commonLabel);
            labelReq.setLabelStatus(CommonStatus.LABEL_STATUS_ENABLE.getKey());
            int i = this.baseMapper.countLabelBusinessType(labelReq);
            if (i >= 3 && CommonStatus.LABEL_STATUS_ENABLE.getKey().equals(commonLabel.getLabelStatus())) {
                throw new BaseException(MessageUtils.message("label.only.three.can.be.enabled.labels"));
            }
            this.baseMapper.insert(commonLabel);
            BrandStoreLabelRela brandStoreLabelRela = new BrandStoreLabelRela();
            brandStoreLabelRela.setLabelId(commonLabel.getLabelId());
            brandStoreLabelRela.setStoreId(commonLabel.getStoreId());
            brandStoreLabelRelaService.save(brandStoreLabelRela);
            String key = CacheConstants.CACHE_BRAND_STORE_LIST + commonLabel.getBrandId();
            redisCache.deleteObject(key);
        }
        return true;
    }

    /**
     * 修改标签
     *
     * @param commonLabelUpdate
     * @return
     */
    @Override
    public boolean edit(CommonLabelUpdate commonLabelUpdate) {
        CommonLabel commonLabel = Convert.convert(new TypeReference<CommonLabel>() {
        }, commonLabelUpdate);
        if (!commonLabel.getBusinessType().equals(LabelType.STORE_LABEL.getKey())) {
            UpdateWrapper<BrandStoreLabelRela> lqw = new UpdateWrapper<>();
            lqw.eq("store_id", commonLabelUpdate.getStoreId());
            lqw.eq("label_id", commonLabelUpdate.getLabelId());
            lqw.set("del_status", Constants.DEL_STATUS_TRUE);
            brandStoreLabelRelaService.update(lqw);
        } else {
            BrandStoreLabelRela brandStoreLabelRela = new BrandStoreLabelRela();
            brandStoreLabelRela.setLabelId(commonLabel.getLabelId());
            brandStoreLabelRela.setStoreId(commonLabel.getStoreId());
            brandStoreLabelRelaService.save(brandStoreLabelRela);
            String key = CacheConstants.CACHE_BRAND_STORE_LIST + commonLabel.getBrandId();
            redisCache.deleteObject(key);
        }
        this.baseMapper.updateById(commonLabel);
        String key = CacheConstants.CACHE_BRAND_STORE_LIST + commonLabel.getBrandId();
        redisCache.deleteObject(key);
        return true;
    }

    /**
     * 删除标签
     *
     * @param idList
     * @return
     */
    @Override
    public boolean delByIds(List<Long> idList, Long brandId) {
        this.removeByIds(idList);
        String key = CacheConstants.CACHE_BRAND_STORE_LIST + brandId;
        redisCache.deleteObject(key);
        return true;
    }
}
